<extend name="Base/common"/>
<block name="Content">
    <div class="am-cf am-padding-top">
        <div class="am-fl am-cf">
            <i class="am-icon-file"></i>
            <strong class="am-text-primary am-text-lg">数据库备份</strong>
        </div>
        <div class="am-text-right am-padding-right">
            <a class="am-btn am-btn-success am-btn-sm" href="javascript:;" autocomplete="off" id="export">备份表</a>
            <a class="am-btn am-btn-secondary am-btn-sm" href="{:U('optimize')}" id="optimize">优化表</a>
            <a class="am-btn am-btn-warning am-btn-sm" href="{:U('repair')}" id="repair">修复表</a>
        </div>
        <hr data-am-widget="divider" class="am-divider am-divider-default" />
    </div>
    <form id="export-form" method="post" action="{:U('export')}" class="am-form">
        <table class="am-table am-table-striped am-table-hover table-main">
            <thead>
                <tr>
                    <th class="table-check am-text-left" style="width:5%">
                        <input type="checkbox" class="checkAll" checked/>
                    </th>
                    <th class="table-type am-text-left" style="width:20%">
                        表名
                    </th>
                    <th class="table-title am-text-left" style="width:10%">
                        数据量
                    </th>
                    <th class="table-set am-text-left" style="width:10%">
                        数据大小
                    </th>
                    <th class="table-set am-text-left" style="width:15%">
                        创建时间
                    </th>
                    <th class="table-set am-text-left" style="width:15%">
                        备份状态
                    </th>
                    <th class="table-set am-text-left" style="width:25%">
                        操作
                    </th>
                </tr>
            </thead>
            <tbody>
                <notempty name="list">
                    <foreach name="list" item="table">
                        <tr>
                            <td>
                                <input type="checkbox" value="{$table.name}" name="tables[]" class="ck" checked/>
                            </td>
                            <td>
                                {$table.name}
                            </td>
                            <td>
                                {$table.rows}
                            </td>
                            <td>
                                {$table.data_length|format_bytes}
                            </td>
                            <td>
                                {$table.create_time}
                            </td>
                            <td class="info">
                                未备份
                            </td>
                            <td>
                                <div class="am-btn-toolbar">
                                    <div class="am-btn-group am-btn-group-xs">
                                        <a class="am-btn am-btn-default am-btn-xs am-text-secondary ajax-get no-refresh" href="{:U('optimize?tables='.$table['name'])}"><span class="am-icon-refresh"></span>优化表</a>
                                        <a class="am-btn am-btn-default am-btn-xs am-text-success ajax-get no-refresh" href="{:U('repair?tables='.$table['name'])}"><span class="am-icon-eraser"></span>修复表</a>
                                    </div>
                                </div>
                            </td>
                        </tr>
                    </foreach>
                    <else/>
                    <td colspan="6" class="text-center">
                        暂时还没有内容! 
                    </td>
                </notempty>
            </tbody>
        </table>
    </form>
    <!-- 分页 -->
    <ul class="am-pagination am-pagination-default am-no-layout amz-social">
        {$_page}
    </ul>
</block>
<block name="script">
    <script type="text/javascript">
        (function($){
            var $form = $("#export-form"), $export = $("#export"), tables
            $optimize = $("#optimize"), $repair = $("#repair");
            
            $optimize.add($repair).click(function(){
                $.post(this.href, $form.serialize(), function(data){
                    if (data.status) {
                        updateAlert(data.info, 'alert-success');
                    }
                    else {
                        updateAlert(data.info, 'alert-error');
                    }
                    setTimeout(function(){
                        $('#top-alert').find('button').click();
                        $(that).removeClass('am-disabled').prop('am-disabled', false);
                    }, 1500);
                }, "json");
                return false;
            });
            
            $export.click(function(){
                $export.parent().children().addClass("am-disabled");
                $export.html("正在发送备份请求...");
                $.post($form.attr("action"), $form.serialize(), function(data){
                    if (data.status) {
                        tables = data.tables;
                        $export.html(data.info + "开始备份，请不要关闭本页面！");
                        backup(data.tab);
                        window.onbeforeunload = function(){
                            return "正在备份数据库，请不要关闭！"
                        }
                    }
                    else {
                        updateAlert(data.info, 'alert-error');
                        $export.parent().children().removeClass("am-disabled");
                        $export.html("立即备份");
                        setTimeout(function(){
                            $('#top-alert').find('button').click();
                            $(that).removeClass('am-disabled').prop('am-disabled', false);
                        }, 1500);
                    }
                }, "json");
                return false;
            });
            
            function backup(tab, status){
                status && showmsg(tab.id, "开始备份...(0%)");
                $.get($form.attr("action"), tab, function(data){
                    if (data.status) {
                        showmsg(tab.id, data.info);
                        
                        if (!$.isPlainObject(data.tab)) {
                            $export.parent().children().removeClass("am-disabled");
                            $export.html("备份完成，点击重新备份");
                            window.onbeforeunload = function(){
                                return null
                            }
                            return;
                        }
                        backup(data.tab, tab.id != data.tab.id);
                    }
                    else {
                        updateAlert(data.info, 'alert-error');
                        $export.parent().children().removeClass("am-disabled");
                        $export.html("立即备份");
                        setTimeout(function(){
                            $('#top-alert').find('button').click();
                            $(that).removeClass('am-disabled').prop('am-disabled', false);
                        }, 1500);
                    }
                }, "json");
                
            }
            
            function showmsg(id, msg){
                $form.find("input[value=" + tables[id] + "]").closest("tr").find(".info").html(msg);
            }
        })(jQuery);
    </script>
</block>
